package mailclient.client;

import java.io.*;
import java.util.ArrayList;
import mailcommon.entities.*;
import mailcommon.helpers.*;

public class Viewer {

    public void showHelp() {
        writeln("Availible commands:");
        for (CommandType c : CommandType.values()) {
            writeln(c.toString());
        }
        for (FunctionType f : FunctionType.values()) {
            writeln(f.toString() + " - " + f.description());
        }
    }
    
    public String requestAddress() 
            throws IOException{
        writeln("Enter your mail address:");
        return readln();
    }

    public void unknownCommand() {
        writeln("Unknown command. Enter \"help\" to see commands.");
    }

    public void createUser() {
        writeln("<lastname> <firstname> <birhdate: yyyy-MM-dd> <telephone>:");
    }

    public void createAddress() {
        writeln("Enter the address you want to register:");
    }

    public User parseUser() throws IOException {
        String info = readln();
        String[] vals = info.split(" ");
        User user = new User(vals[0], vals[1],
                DateHelper.getInstance().parse(vals[2]), vals[3]);
        return user;
    }

    public void write(String s) {
        System.out.print(s);
    }

    public void writeln(String s) {
        System.out.println(s);
    }

    public String readln()
            throws IOException {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(System.in));
        String s = reader.readLine();

        return s;
    }

    public String[] createLetter()
            throws IOException {
        writeln("Enter recipient:");
        String recipient = readln();
        writeln("Enter subject:");
        String subject = readln();
        writeln("Enter body:");
        String body = readln();

        String[] vals = {recipient, subject, body};
        return vals;
    }

    public void showLetters(ArrayList<Letter> letters) {
        if (letters.isEmpty()) {
            writeln("No letters.");
        } else {
            for (Letter l : letters) {
                writeln(l.getId() + " " + l.getSenderAddress() + " " 
                        + l.getDateString() + " " + l.getSubject());
            }
        }
    }

    public void requestLetterId() {
        writeln("Enter id of letter you want to read:");
    }

    public int getLetterId()
            throws IOException {
        String s = readln();
        return Integer.parseInt(s);
    }

    public void showLetter(Letter letter) {
        writeln("ID : " + letter.getId());
        writeln("SENDER : " + letter.getSenderAddress());
        writeln("DATE : " + letter.getDateString());
        writeln("SUBJECT : " + letter.getSubject());
        writeln("BODY : " + letter.getBody());
    }
}
